package alo

import (
	"gitee.com/ChanYeung/go-utils/debug"
)

func AvgBucket(list []int, m int) {
	bucketList := []*Bucket{}
	//初始化每个桶
	for i := 0; i < m; i++ {
		bucketList = append(bucketList, &Bucket{})
	}
	//桶分组
	for i := len(list) - 1; i >= 0; i-- {
		//从高位往地位遍历
		//找到最小桶
		index := getMinBucket(bucketList)
		bucketList[index].Sum += list[i]
		bucketList[index].Ele = append(bucketList[index].Ele, list[i])
	}
	//打印结果
	for i := range bucketList {
		//fmt.Printf("bucket index=%v elems[]")
		debug.PrintArray(bucketList[i].Ele)
	}
}
func getMinBucket(buckets []*Bucket) int {
	index := 0
	minAvg := 0
	for i := range buckets {
		if buckets[i].Sum < minAvg {
			index = i
			minAvg = buckets[i].Sum
		}
	}
	return index
}

type Bucket struct {
	Ele []int
	Sum int
}
